//
//  ChooseSchoolViewController.m
//  IpadR
//
//  Created by edianzu on 2017/5/25.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "ChooseSchoolViewController.h"
#import "MainViewController.h"
#import "SchoolModel.h"
@interface ChooseSchoolViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)SchoolModel * sModel;
@property(nonatomic,strong)NSMutableArray * schoolNameArr;//名字数组
@property(nonatomic,strong)NSMutableArray * ZiMuArr;//右边INDEX的字母
@property(nonatomic,strong)NSMutableArray * ZiMuPaiXuArr;//存放经过首字母排序的名字数组的数组
@end

@implementation ChooseSchoolViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataArray = [[NSMutableArray alloc]init];
    self.ZiMuArr = [[NSMutableArray alloc]init];
    self.schoolNameArr=[[NSMutableArray alloc]init];
    self.ZiMuPaiXuArr = [[NSMutableArray alloc]init];
    
    NSString* str = [[NSUserDefaults standardUserDefaults]objectForKey:@"AllData"];//获取全部数据
//    NSLog(@"eee===%@",str);
    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSArray * schoolArr = [[NSArray alloc]init];
    schoolArr = dict[@"Kindergartens"];
            for (NSDictionary * dic in schoolArr) {
                self.sModel = [SchoolModel modelWithDictionary:dic];
                [self.dataArray addObject:self.sModel];
                [self.schoolNameArr addObject:self.sModel.KindergartenName];
            }
    
    _ZiMuArr = [ChineseString IndexArray:self.schoolNameArr];
    _ZiMuPaiXuArr = [ChineseString LetterSortArray:self.schoolNameArr];
//    NSLog(@"dataarr == %ld,scname == %ld,zimu == %ld",_dataArray.count, _schoolNameArr.count,_ZiMuArr.count);
//    for (NSString* str in _ZiMuPaiXuArr) {
//        NSLog(@"zimupaixu ==%@",str);
//    }
//    for (NSString* str in _ZiMuArr) {
//        NSLog(@"zimu ==%@",str);
//    }
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title= @"选择学校";
     
    [self creatTable];
}

-(void)creatTable{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableView.delegate =self;
    _tableView.dataSource =self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tableView];
    
}
//禁止TABLEVIEW 下拉顶部超过SCREEN
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    NSLog(@"%f",_mainView.myTableView.contentOffset.y);
    if (_tableView.contentOffset.y <= 0) {
        _tableView.bounces = NO;
        NSLog(@"禁止下拉");
    }
    else
        if (_tableView.contentOffset.y >= 0){
            _tableView.bounces = YES;
            NSLog(@"允许上拉");
            
        }
}
#pragma mark TableViewDelegate

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.ZiMuArr;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.ZiMuArr objectAtIndex:section];
    return key;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _ZiMuPaiXuArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.ZiMuPaiXuArr objectAtIndex:section] count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100*AutoScaleVertical;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = _ZiMuPaiXuArr[indexPath.section][indexPath.row];
    UIView * view = [[UIView alloc]init];//cell间隔线
    view.backgroundColor= UIColorFromRGB(247, 247, 247);
    [cell.contentView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(cell.contentView.mas_bottom);
        make.left.mas_equalTo(cell.contentView.mas_left);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 1*AutoScaleVertical));
    }];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    MainViewController * mvc = [[MainViewController alloc]init];
    [ user setObject:_ZiMuPaiXuArr[indexPath.section][indexPath.row] forKey:@"MainTitle"];
//    mvc.titleStr = _ZiMuPaiXuArr[indexPath.section][indexPath.row];
    for (_sModel in _dataArray) {
        NSLog(@"%@ === %@",_sModel.KindergartenName,_ZiMuPaiXuArr[indexPath.section][indexPath.row]);
        if ([_sModel.KindergartenName isEqualToString:_ZiMuPaiXuArr[indexPath.section][indexPath.row] ]) {
            
//            mvc.classTag = _sModel.KindergartenId;
            [user setObject:_sModel.KindergartenId forKey:@"SchoolId"];
        }
    }
    
//    [self.navigationController pushViewController:mvc animated:YES];
    
    UINavigationController * navi = [[UINavigationController alloc]initWithRootViewController:mvc];
    navi.navigationBar.barTintColor = [HexColor colorWithHexString:Green_Main];
    navi.navigationBar.translucent = NO;
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    [self.navigationController presentViewController:navi animated:YES completion:nil];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
